﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using ProjectWizardManagement.Entity.Model;
using ProjectWizardManagement.Entity.Repository.Abstract;

namespace ProjectWizardManagement.Entity.Repository.Concrete
{
    public class EFUserRepository : IUserRepository
    {
        public IQueryable<User> Users
        {
            get { return Context.Users; }
        }

        public void SaveUser(User user)
        {
            if (user.Id == 0)
                Context.Users.Add(user);
            else
                Context.Entry(user).State = EntityState.Modified;
            Context.SaveChanges();
        }

        public void DeleteUser(User user)
        {
            var dbUser = Users.SingleOrDefault(x => x.Id == user.Id);

            if (dbUser != null)
                Context.Users.Remove(dbUser);
            Context.SaveChanges();
        }

        public User FindUser(int id)
        {
            return Users.FirstOrDefault(x => x.Id == id);
        }

        public User FindUserByUsername(string username)
        {
            return Users.FirstOrDefault(x => x.Username == username);
        }

        public EFDbContext Context { get; set; } 
    }
}
